package com.century.sjt.widget.TitleBar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.century.sjt.R;

/**
 * Created by 炳基 on 2016/7/7.
 * TitleBar
 */
public class ChatTitleBar extends LinearLayout {
    protected LinearLayout leftLayout;
    protected TextView leftIcon;
    private TextView rightIcon1,rightIcon2;
    protected TextView titleView;
    protected LinearLayout titleLayout;
    protected LinearLayout rightLayout;
    protected LinearLayout rightlayoutIcon1,rightlayoutIcon2;

    public ChatTitleBar(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ChatTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ChatTitleBar(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.sjt_chattitlebar, this);
        leftLayout = (LinearLayout) findViewById(R.id.left_layout);
        leftIcon = (TextView) findViewById(R.id.left_icon);
        rightLayout = (LinearLayout) findViewById(R.id.right_layout);
        rightIcon1 = (TextView) findViewById(R.id.right_icon1);
        rightIcon2 = (TextView) findViewById(R.id.right_icon2);
        rightlayoutIcon1 = (LinearLayout) findViewById(R.id.rightlayout_icon1);
        rightlayoutIcon2 = (LinearLayout) findViewById(R.id.rightlayout_icon2);
        titleView = (TextView) findViewById(R.id.title);
        titleLayout = (LinearLayout) findViewById(R.id.root);
        Typeface iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf");
        rightIcon1.setTypeface(iconfont);
        rightIcon2.setTypeface(iconfont);
        leftIcon.setTypeface(iconfont);

        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs){
        if(attrs != null){
            TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.ChatTitleBar);
            String title = ta.getString(R.styleable.ChatTitleBar_MytitleBarTitle);
            titleView.setText(title);

            String mlefticon = ta.getString(R.styleable.ChatTitleBar_MytitleBarLeftIcon);
            if (null != mlefticon) {
                leftIcon.setText(mlefticon);
            }
            String mrighticon1 = ta.getString(R.styleable.ChatTitleBar_MytitleBarRightIcon1);
            if (null != mrighticon1) {
                rightIcon1.setText(mrighticon1);
            }
            String mrighticon2 = ta.getString(R.styleable.ChatTitleBar_MytitleBarRightIcon2);
            if (null != mrighticon2) {
                rightIcon2.setText(mrighticon2);
            }
            Drawable background = ta.getDrawable(R.styleable.ChatTitleBar_MytitleBarBackground);
            if(null != background) {
//                titleLayout.setBackgroundDrawable(background);
            }

            ta.recycle();
        }
    }

    public void setLeftIconResource(String resId) {
        leftIcon.setText(resId);
    }

    public void setRightIcon1Resourc(String resId) {
        rightIcon1.setText(resId);
    }
    public void setRightIcon2Resourc(String resId) {
        rightIcon2.setText(resId);
    }
    public void setRightIcon2ClickListener(OnClickListener listener){
        rightlayoutIcon2.setOnClickListener(listener);
    }
    public void setRightIcon1ClickListener(OnClickListener listener){
        rightlayoutIcon1.setOnClickListener(listener);
    }
    public void setLeftLayoutClickListener(OnClickListener listener){
        leftLayout.setOnClickListener(listener);
    }


    public void setRightLayoutClickListener(OnClickListener listener){
        rightLayout.setOnClickListener(listener);
    }
    public void setTitle(String title){
        titleView.setText(title);
    }

    public void setBackgroundColor(int color){
        titleLayout.setBackgroundColor(color);
    }

    public LinearLayout getLeftLayout(){
        return leftLayout;
    }

    public LinearLayout getRightLayout(){
        return rightLayout;
    }
}
